import Link from 'next/link'
import { getAllPosts, type Post } from '@/lib'

export default async function TagsPage() {
  const posts = await getAllPosts()
  const tags = Array.from(new Set(posts.flatMap(post => post.tags)))
  
  const postsByTag = tags.reduce((acc, tag) => {
    acc[tag] = posts.filter(post => post.tags.includes(tag))
    return acc
  }, {} as Record<string, typeof posts>)

  return (
    <main className="min-h-screen p-4 md:p-8">
      <div className="max-w-4xl mx-auto">
        <h1 className="text-4xl font-bold mb-8 dark:text-white">标签</h1>
        
        <div className="grid gap-8">
          {tags.map(tag => (
            <section key={tag} className="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
              <h2 className="text-2xl font-semibold mb-4 dark:text-white">
                #{tag} ({postsByTag[tag].length})
              </h2>
              <div className="space-y-4">
                {postsByTag[tag].map(post => (
                  <article key={post.id} className="border-b border-gray-200 dark:border-gray-700 pb-4 last:border-0">
                    <h3 className="text-xl font-medium mb-2">
                      <Link href={`/posts/${post.id}`} className="hover:text-blue-600 dark:text-gray-200 dark:hover:text-blue-400">
                        {post.title}
                      </Link>
                    </h3>
                    <p className="text-gray-600 dark:text-gray-300">{post.description}</p>
                    <div className="mt-2 text-sm text-gray-500 dark:text-gray-400">
                      发布于 {post.date}
                    </div>
                  </article>
                ))}
              </div>
            </section>
          ))}
        </div>
      </div>
    </main>
  )
} 